<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/6/12
 * Time: 9:59
 */
namespace app\admin\controller;
use think\Db;
class Financial extends Base {

    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = array();
            //日期
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['revenue_time'] = ['between', [$params['add_start'], $params['add_end']]];
            }
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] == ''){
                $where['revenue_time'] = ['>=', $params['add_start']];
            }if(isset($params['add_start']) && $params['add_start'] == '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['revenue_time'] = ['<=', $params['add_end']];
            }

            //状态
            if(isset($params['revenue_type']) && $params['revenue_type'] != ''){
                $where['revenue_type'] = $params['revenue_type'];
            }
            //类型
            if(isset($params['revenue_status']) && $params['revenue_status'] != ''){
                $where['revenue_status'] = $params['revenue_status'];
            }

            if(isset($params['revenue_money']) && $params['revenue_money'] != ''&&$params['revenue_money']==1){
                $where['revenue_price'] = array('>=',0.01);
//                $where['revenue_glod'] = null;
            }

            if(isset($params['revenue_money']) && $params['revenue_money'] != ''&&$params['revenue_money']==2){
                $where['revenue_glod'] = array('>=',0.01);
//                $where['revenue_price'] = null;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('revenue')->alias('a')
                ->where($where)->count();// 获取总条数
            $count2 = Db::name('revenue')->alias('a')
                ->where($where)->column('revenue_id');// 获取总条数
            $lists = Db::name('revenue')->alias('a')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('revenue_id desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['revenue_glod']!=0&&$vo['revenue_price']!=''){
                    $vo['price_name'] = 'RMB + 金币';
                    $vo['price'] = $vo['revenue_price'].' + '.$vo['revenue_glod'];
                }elseif ($vo['revenue_glod']!=''&&$vo['revenue_price']==0){
                    $vo['price_name'] = '金币';
                    $vo['price'] = $vo['revenue_glod'];
                }elseif($vo['revenue_price']!=''&&$vo['revenue_glod']==0) {
                    $vo['price_name'] = 'RMB';
                    $vo['price'] = $vo['revenue_price'];
                }
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        if(session('?order_id10')){
            session('order_id10',null);
        }
        return  $this->fetch();
    }

    /***
     * 批量导出
     */
    public function excelIn(){
        $ids = input('param.ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
        $where['a.revenue_id'] = array('IN',$ids);
        $lists = Db::name('revenue')->alias('a')
            ->where($where)
            ->order('revenue_id desc')
            ->select();
        foreach ($lists as $key=>&$vo){
            if($vo['revenue_glod']!==''&&$vo['revenue_price']!==''){
                $vo['price_name'] = 'RMB + 金币';
                $vo['price'] = $vo['revenue_price'].' + '.$vo['revenue_glod'];
            }elseif ($vo['revenue_glod']!==''&&$vo['revenue_price']===''){
                $vo['price_name'] = '金币';
                $vo['price'] = $vo['revenue_glod'];
            }elseif($vo['revenue_price']!==''&&$vo['revenue_glod']==='') {
                $vo['price_name'] = 'RMB';
                $vo['price'] = $vo['revenue_price'];
            }
            if($vo['revenue_status']==1){
                $vo['revenue_status_name'] = '收入';
            }elseif ($vo['revenue_status']==2){
                $vo['revenue_status_name'] = '支出';
            }
            if($vo['revenue_type']==1){
                $vo['revenue_type_name'] = '订单';
            }elseif ($vo['revenue_type']==2){
                $vo['revenue_type_name'] = '研学活动';
            }
            unset($lists[$key]['revenue_id']);
            unset($lists[$key]['revenue_name']);
            unset($lists[$key]['revenue_type']);
            unset($lists[$key]['revenue_status']);
            unset($lists[$key]['revenue_price']);
            unset($lists[$key]['revenue_glod']);
        }
        $cellname = [
            ['revenue_time','营收时间',20,'LEFT'],
            ['revenue_gl_id','营收项',15,'LEFT'],
            ['price_name','营收币种',15,'LEFT'],
            ['price','营收面额',15,'LEFT'],
            ['revenue_status_name','类型',15,'LEFT'],
        ];
        
        $res = exportExcel('财务管理列表','',$cellname,$lists);
        return json($res);
    }


    //订单
    public function detail(){
        if(request()->isAjax()){
            $id = input('param.id');
            $where['oi_order_id'] = $id;
            $count = Db::table('up_order_info')->where($where)->count();
            $lists = Db::table('up_order_info')->where($where)->select();
            foreach ($lists as $key=>&$vo){
                if($vo['oi_type']==1){
                    $info = Db::table('up_good_spec')->where('spec_id',$vo['oi_spec_id'])->find();
                    $vo['spec'] = $info['spec_name'];
                    $vo['price'] = $vo['oi_price']*$vo['oi_num'].'人民币';
                }else{
                    $vo['spec'] = '';
                    $vo['price'] = $vo['oi_price']*$vo['oi_num'].'金币';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $id = input('id');
        if($id != ''){
            session('order_id10',$id);
        }
        return $this->fetch();
    }

    //研学活动
    //查看详情
    public function details(){
        $id = input('param.id');
        $res = Db::table('up_notice')->where('n_id',$id)->find();
        $res['school'] = explode(',',trim($res['n_school'],','));
        $this->assign('res',$res);
        $school = Db::table('up_school')
            ->where('school_status', 1)
            ->where('school_del', 1)
            ->field('school_id, school_name')
            ->select();
        $this->assign([
            'school' => $school
        ]);
        return $this->fetch();
    }
}